package com.xb.xbpm.controller;

import com.xb.xbpm.service.IDataService;
import com.xb.xbpm.service.ISiteService;
import com.xb.xbpm.utils.Constants;
import com.xb.xbpm.utils.EsUtils;
import com.xb.xbpm.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 *
 * 从数据库获取pv uv ips等数据
 * @Author gxd
 * @Date： 2018/7/23
 */
@Controller
@RequestMapping(value = "da")
public class DataController {

    @Autowired
    EsUtils esUtils;

    @Autowired
    ISiteService siteService;
    @Autowired
    IDataService dataService;

    @RequestMapping("/getTotalList")
    public @ResponseBody
    Map<String, Object> getTotalList(HttpServletRequest request){
        ModelMap map=new ModelMap();

        long pv=0;
        long uv=0;
        long ov=0;//新增独立
        long ip=0;
        double pc=0;//人均浏览页数
        List<Map<String, Object>> pvList=new ArrayList<Map<String,Object>>();
        List<Map<String, Object>> uvList=new ArrayList<Map<String,Object>>();
        List<Map<String, Object>> ipList=new ArrayList<Map<String,Object>>();
        //新独立访客
        List<Map<String, Object>> ovList=new ArrayList<Map<String,Object>>();

        int timeType=0;//0:当天 查小时间隔 1:多天查 日间隔

        String startDate="";
        String endDate="";
        String siteid= request.getParameter("siteid");
        String date= request.getParameter("date");
        String userid= request.getParameter("userid");

        if(Tools.isEmpty(siteid)){
            List<Map<String,Object>> list1= new ArrayList<Map<String,Object>>();
            list1=siteService.getSiteList(userid);
            if(list1.size()>0){
                siteid=list1.get(0).get("id").toString();
            }else{
                map.put("pv",pv);
                map.put("uv",uv);
                map.put("ov",ov);
                map.put("ip",ip);
                map.put("pc",pc);
                map.put("pvList",pvList);
                map.put("uvList",uvList);
                map.put("ipList",ipList);
                map.put("ovList",ovList);//新增独立访客
                return map;
            }
        }

        if(Tools.notEmpty(date)){//日期不为空
            String [] strs=date.split(",");
            startDate=strs[0];
            endDate=strs[1];
            if(!startDate.equals(endDate)){
                timeType=1;
            }
        }else{//默认当天
            startDate=Tools.date2Str(new Date(),"yyyy-MM-dd");
        }
        pv=dataService.getTotalCountPv(userid,siteid,timeType,startDate,endDate);
        uv=dataService.getTotalCountUv(userid,siteid,timeType,startDate,endDate);
        ip=dataService.getTotalCountIps(userid,siteid,timeType,startDate,endDate);
        ov=dataService.getTotalCountNewUv(userid,siteid,timeType,startDate,endDate);
        if(uv>0){
            pc=pv/uv;//pv/uv
        }

        pvList=dataService.getCountDataList(Constants.COUNT_PV,userid,siteid,timeType,startDate,endDate);
        uvList=dataService.getCountDataList(Constants.COUNT_UV,userid,siteid,timeType,startDate,endDate);
        ipList=dataService.getCountDataList(Constants.COUNT_IPS,userid,siteid,timeType,startDate,endDate);
        ovList=dataService.getCountDataList(Constants.COUNT_NEWUV,userid,siteid,timeType,startDate,endDate);

        map.put("pv",pv);
        map.put("uv",uv);
        map.put("ov",ov);
        map.put("ip",ip);
        map.put("pc",pc);
        map.put("pvList",pvList);
        map.put("uvList",uvList);
        map.put("ipList",ipList);
        map.put("ovList",ovList);//新增独立访客
        return map;
    }






}
